
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.AlertType;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.List;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;

import javax.microedition.lcdui.TextBox;



 
 
public class Xapp extends MIDlet implements CommandListener, Runnable{


	private Display display;
	private Form addressForm;
	private Form connectForm;
	private List selection;
    private TextField serverName;
    private TextField serverPort;
	private TextField groupId;
	private TextField tempMax;
    private Command okCommand;
    private Command exitCommand;
    private Command backCommand;
	private Command startCommand;
	private Command selectCommand;
	
	private TextBox debug;
	
	private DataRecord db;
	private PacketReader pr;
	
    protected void startApp() {
        if (display == null) {
            initialize();
            display.setCurrent(addressForm);
        }
    }

    protected void pauseApp() {
    }

    protected void destroyApp(boolean unconditional) {
		notifyDestroyed();
    }

    public void commandAction(Command cmd, Displayable d) {
	
		String label = cmd.getLabel();
        if (label.equals("Ok")){
		
			display.setCurrent(connectForm);
			
		} else if(label.equals("Start")){
		
			display.setCurrent(selection);
			
		    String server= serverName.getString();
			String port= serverPort.getString();
			String group= groupId.getString();
			db=new DataRecord(group);
			pr=new PacketReader(this, db, server, port);
			new Thread(this).start();
			
            pr.start();
			
        } else if (label.equals("Back")) {
		
            display.setCurrent(addressForm);
		
        } else if (label.equals("Select")){
		
			
				switch(selection.getSelectedIndex()){
				
					case 0 :// display
							display.setCurrent(pr.getDisplay());
							break;
					
					case 1 ://command
							break;		
				}
        
		} else if ( label.equals("Stop")){
		
			display.setCurrent(addressForm);
			pr.close();
		
		} else if ( label.equals("Return")){
		
			display.setCurrent(selection);
			
		}else if (label.equals("Exit")) {
            try {
                destroyApp(true);
            } catch (Exception ex) {
			}
            notifyDestroyed();
        }
    }

	Form getAdressForm(){
		return addressForm;
	}
	private void initialize() {
	
        display = Display.getDisplay(this);

        // Commands
        exitCommand = new Command("Exit", Command.EXIT, 0);
        okCommand = new Command("Ok", Command.OK, 0);
        backCommand = new Command("Back", Command.BACK, 0);
		startCommand = new Command("Start", Command.OK, 0);
		selectCommand= new Command("Select", Command.ITEM, 1);


        // The address form
        addressForm = new Form("Socket Client");
        serverName = new TextField("Server name:", "", 256, TextField.ANY);
        serverPort = new TextField("Server port:", "", 8, TextField.NUMERIC);
        addressForm.append(serverName);
        addressForm.append(serverPort);
        addressForm.addCommand(okCommand);
        addressForm.addCommand(exitCommand);
        addressForm.setCommandListener(this);
	
        // The connect form
        connectForm = new Form("Connecting");
	    groupId = new TextField("group id:", "", 8, TextField.NUMERIC);
		tempMax = new TextField("Max temp:", "", 8, TextField.NUMERIC);
        connectForm.append(groupId);
		connectForm.append(tempMax);
        connectForm.addCommand(backCommand);
	    connectForm.addCommand(startCommand);
        connectForm.setCommandListener(this);
		
		// The selection list
		String choix[] = {"Display", "Command"};
		selection = new List("Faites un choix", List.IMPLICIT, choix, null);
		selection.addCommand(selectCommand);
		selection.addCommand(backCommand);		
		selection.setCommandListener(this);
		
		// debug form
		debug = new TextBox("debug", "", 2048, TextField.ANY );
		
		
       
    }
	public void run() {
	
		display.setCurrent(debug);
	     try{
	       while(true){
		         
				String[] liste=db.getList();
				for(int i=0; i<liste.length; i++ ){
				//System.out.println("Xapp");
				//System.out.println(liste.length);
				String tempMax=db.getMax(liste[i]);
				debug.setString(liste[i] + " "+tempMax);
		         Thread.sleep(1000);
				}

		   }
		   
		}catch(Exception e){
		
		/*
           Alert alert = new Alert("Error ",
                        "An error occurred while communicating with the server.", 
						null, AlertType.ERROR);
            alert.setTimeout(Alert.FOREVER);
            display.setCurrent(alert, parent.getAdressForm());

			*/	
		}
			
    }	
}
